#include "systreedirview.h"
#include <QTreeView>
#include "Share/dbSqlGlobal.h"
#include "mainwindow.h"

CNS_SYS_REGISTER(SysTreeDirView)

SysTreeDirView::SysTreeDirView()
{

}


void SysTreeDirView::f_createView(CnsComponentPtr com, CnsComponentPtr mainView)
{
    MainWindowPtr w = mainView->property("mainWindow").value<MainWindowPtr>();
    const QString &viewName = com->propertyInNs("viewName").toString();
    CnsFileSystemModelPtr model = com->propertyInNs("model").value<CnsFileSystemModelPtr>();
    QTreeView* tree = w->findChild<QTreeView*>(viewName);

    const QString &rootPath = com->propertyInNs("rootPath").toString();
    CnsSortFilterProxyModelPtr proxyModel = com->propertyInNs("proxyModel").value<CnsSortFilterProxyModelPtr>();
    const QString &suffix = com->propertyInNs("suffix").toString();
//    model->setNameFilters(QStringList() << suffix);
    proxyModel->setRecursiveFilteringEnabled(true);
//    proxyModel->setFilterRole(QFileSystemModel::FileNameRole);

    proxyModel->setSourceModel(model.data());
    proxyModel->setFilterWildcard(suffix);
    tree->setModel(proxyModel.data());
    if (!rootPath.isEmpty()) {
        const QModelIndex rootIndex = model->index(QDir::cleanPath(rootPath));
        if (rootIndex.isValid()){
            const QModelIndex proxy_index = proxyModel->mapFromSource(rootIndex);
            bool isValid = proxy_index.isValid();
            tree->setRootIndex(proxy_index);
        }
    }

    // Demonstrating look and feel features
    tree->setAnimated(false);
    tree->setIndentation(20);
    tree->setSortingEnabled(true);
//    const QSize availableSize = QApplication::desktop()->availableGeometry(&tree).size();
//    tree->resize(availableSize / 2);
    tree->setColumnWidth(0, tree->width() / 3);

    tree->setWindowTitle(QObject::tr("Dir View"));
    tree->show();
}
